/**  
 * Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
 * in source and binary forms, with or without modification, are permitted  
 * provided that the following conditions are met: Redistributions of source  
 * code must retain the above copyright notice, this list of conditions and the  
 * following disclaimer. Redistributions in binary form must reproduce the above  
 * copyright notice, this list of conditions and the following disclaimer in the  
 * documentation and/or other materials provided with the distribution. Neither  
 * the name of Noel Lynch nor the names of contributors may be used to  
 * endorse or promote products derived from this software without specific prior  
 * written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 */
// Oct 24, 2007

package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import net.miginfocom.swing.MigLayout;

import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class ResourceLocatorPanel extends JPanel
{
	private static final String ADD_RESOURCE = "Add Resource";

	public		File		mcl_resLoc = null;

	//public		JLabel		mcl_resLocLbl;

	public		JButton		mcl_resLocBtn;

	public		JComboBox	mcl_resTypes;
	private		JTextArea	mcl_loc;

	public 
	ResourceLocatorPanel(final JDialog cl_par) {
		setLayout(new MigLayout("inset 5"));

		mcl_resLocBtn = SwingComponentFactory.makeAButton("Locate Base");
		mcl_resLocBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_file = GenericUtils.getAFile(null, true, false);
				if(lcl_file == null) { return; }

				mcl_resLoc = lcl_file;
				mcl_loc.setText(mcl_resLoc.toString());
				return;
			}
			
		});
		add(mcl_resLocBtn);

		JButton		lcl_addBtn = SwingComponentFactory.makeAButton(ADD_RESOURCE);
		lcl_addBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(mcl_resLoc == null) { return; }

					SimpleResourceLocator	lcl_loc = new SimpleResourceLocator(mcl_resLoc.toURL());

					String	lstr_resType = (String)mcl_resTypes.getSelectedItem();

					if(lstr_resType.equals("TYPE_TEXTURE")) {	
						ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, lcl_loc);
						SimpleAlert.doAlert("Resource Added " + mcl_resLoc.toURL(), null);
						//System.out.println("ResourceLocatorPanel : added " + lcl_dlg.mcl_resLoc.toURL() + " : " + lstr_resType);
						return;
					}

					if(lstr_resType.equals("TYPE_MODEL")) {
						ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_MODEL, lcl_loc);
						SimpleAlert.doAlert("Resource Added " + mcl_resLoc.toURL(), null);
						return;
					}

					if(lstr_resType.equals("TYPE_PARTICLE")) {
						ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_PARTICLE, lcl_loc);
						SimpleAlert.doAlert("Resource Added " + mcl_resLoc.toURL(), null);
						return;
					}

					if(lstr_resType.equals("TYPE_SHADER")) {
						ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_SHADER, lcl_loc);
						SimpleAlert.doAlert("Resource Added " + mcl_resLoc.toURL(), null);
						return;
					}

					if(lstr_resType.equals("TYPE_AUDIO")) {
						ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_AUDIO, lcl_loc);
						SimpleAlert.doAlert("Resource Added " + mcl_resLoc.toURL(), null);
						return;
					}

				} 
				catch (Exception cl_ex) {
					cl_ex.printStackTrace();
				}
			}
			
		});
		add(lcl_addBtn);

		String	lstra_resTypes[] = { "TYPE_TEXTURE", "TYPE_MODEL", "TYPE_PARTICLE", "TYPE_AUDIO", "TYPE_SHADER" };
		mcl_resTypes = SwingComponentFactory.makeAComboBox();
		add(mcl_resTypes, "wrap");
		
		DefaultComboBoxModel		lcl_model = new DefaultComboBoxModel(lstra_resTypes);
		mcl_resTypes.setModel(lcl_model);
		mcl_resTypes.setSelectedIndex(0);

		mcl_loc = new JTextArea();
		mcl_loc.setEditable(false);
		mcl_loc.setLineWrap(true);
		add(mcl_loc, "width 100%, height 100%, wrap, span");
		
		JButton		lcl_done = SwingComponentFactory.makeAButton("Done");
		lcl_done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_par.dispose();
			}
		});
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_done);
	}

}
